/**==============================================================================
            Copyright (c) 2013 Moritz Hader, Marcel Schrder
            All Rights Reserved.

	@file  ThreadSaveList.java

	@brief  Implementation of the Collection-Interface that ensures Thread-safety
	when accessing a linked-list object
   
   This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

==============================================================================*/

package de.ibm.dhbw.heimdall.engine.math;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;

public class ThreadSaveList<E> implements Collection<E>{
	
	private Object SafeLock = new Object();
	private LinkedList<E> localList;
	
	public ThreadSaveList(){
		this.localList = new LinkedList<E>();
	}
	
	public E get(int index){
		synchronized (SafeLock) {
			return this.localList.get(index);
		}
	}
	
	public E getFirst(){
		synchronized (SafeLock) {
			return this.localList.getFirst();
		}
	}
	
	public E getLast(){
		synchronized (SafeLock) {
			return this.localList.getLast();
		}
	}

	@Override
	public boolean add(Object object) {
		synchronized (SafeLock) {
			return this.localList.add((E) object);
		}
	}

	@Override
	public boolean addAll(Collection arg0) {
		synchronized (SafeLock) {
			return this.localList.addAll(arg0);
		}
	}

	@Override
	public void clear() {
		synchronized (SafeLock) {
			this.localList.clear();
		}
	}

	@Override
	public boolean contains(Object object) {
		synchronized (SafeLock) {
			return this.localList.contains(object);
		}
	}

	@Override
	public boolean containsAll(Collection arg0) {
		synchronized (SafeLock) {
			return this.localList.containsAll(arg0);
		}
	}

	@Override
	public boolean isEmpty() {
		synchronized (SafeLock) {
			return this.localList.isEmpty();
		}
	}

	@Override
	public Iterator iterator() {
		synchronized (SafeLock) {
			return this.localList.iterator();
		}
	}

	@Override
	public boolean remove(Object object) {
		synchronized (SafeLock) {
			return this.localList.remove(object);
		}
	}

	@Override
	public boolean removeAll(Collection arg0) {
		synchronized (SafeLock) {
			return this.localList.removeAll(arg0);
		}
	}

	@Override
	public boolean retainAll(Collection arg0) {
		synchronized (SafeLock) {
			return this.localList.retainAll(arg0);
		}
	}

	@Override
	public int size() {
		synchronized (SafeLock) {
			return this.localList.size();
		}
	}

	@Override
	public Object[] toArray() {
		synchronized (SafeLock) {
			return this.localList.toArray();
		}
	}

	@Override
	public Object[] toArray(Object[] array) {
		synchronized (SafeLock) {
			return this.localList.toArray(array);
		}
	}
	
	
}
